/*
Illuminator (LED drawing tool for iPhone)
Copyright (C) 2008
Nik S Denin
Ivan A Vlasov
Dmitriy A Jackovlev

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#import "TwitterView.h"
#import "MainView.h" // this is for delegate methods

@implementation TwitterView

- (id)initWithFrame:(CGRect)frame
{

	if ((self = [super initWithFrame:frame]) != nil)
	{
		m_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)];
		[m_navBar setBarStyle:0];
		
		[m_navBar showButtonsWithLeftTitle:@"Cancel" rightTitle:@"Post" leftBack:YES];

		UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Twitter"] autorelease];
		[m_navBar pushNavigationItem:navitem];
		
		[m_navBar setDelegate:self];
		[m_navBar enableAnimation];
		[self addSubview:m_navBar];
		
		frame.origin.y += 40.0f;
		frame.size.height -= 40.0f;
		
		[self constructCells];
		m_mainTable = [[UIPreferencesTable alloc] initWithFrame:frame];
		[m_mainTable setDelegate:self];
		[m_mainTable setDataSource:self];
		[m_mainTable setScrollingEnabled:YES];
		[self addSubview:m_mainTable];
		[m_mainTable reloadData];
		[m_mainTable setReusesTableCells:YES];
	}
	return self;
}

- (int)preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group
{
	switch (group)
	{
		case 0: return 1; break; // cell with Status Message
		case 1: return 1; break; // cell with Twitter Username
		case 2: return 1; break; // cell with Twitter password
		default: break;
	}
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForGroup:(int)group
{
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	switch (group)
	{
		case 0: [cell setTitle:@"Status Message"]; break; // cell with Status Message
		case 1: [cell setTitle:@"Twitter username"]; break; // cell with Twitter Username
		case 2: [cell setTitle:@"Twitter password"]; break; // cell with Twitter password
		default: [cell setTitle:@""]; break;
	}
// 	[cell setEnabled:NO];
	return [cell retain];
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group
{
	switch (group)
	{
		case 0: // message cell
			return m_messageCell;
			break;
		case 1: // username cell
			return m_loginCell;
			break;
		case 2: // password cell
			return m_passwordCell;
			break;
		default:
			break;
	}
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	return cell;
}


- (float)preferencesTable:(UIPreferencesTable *)table heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed
{
	
	if (group == 0 && row == -1) return 40.;
	if (group == 0 && row == 0) return 40.;
	
	if (group == 1 && row == -1) return 40.;
	if (group == 1 && row == 0) return 40.;
	
	if (group == 2 && row == -1) return 40.;
	if (group == 2 && row == 0) return 40.;
	
	return proposed;
}

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table
{
	return 3;
}

- (void)setDelegate:(id)delegate
{
	m_delegate = delegate;
}

- (void)navigationBar:(UINavigationBar *)navBar buttonClicked:(int)button
{
	switch(button)
	{
		case 1: // back button pressed
			[m_delegate showDrawField];
			break;
		case 0: // post button pressed
		{
			TRACE(@"POST clicked");
			[TwitPicAPI uploadAndPostWithFile:@"/tmp/foo_0.png" 
				withUsername:[[m_loginCell textField] text]
				withPassword:[[m_passwordCell textField] text]
				withMessage:[[m_messageCell textField] text]];
			[m_delegate showDrawField];
			
		}
			break;
		default:
			break;
	}
}

- (void)constructCells
{
	m_messageCell = [[UIPreferencesTextTableCell alloc] init];
	[m_messageCell setValue:@"Check this out!"];
	[[m_messageCell textField] setAutoCapsType:0];

	m_loginCell = [[UIPreferencesTextTableCell alloc] init];
	[m_loginCell setValue:@""];
	[[m_loginCell textField] setAutoCapsType:0];
	
	m_passwordCell = [[UIPreferencesTextTableCell alloc] init];
	[m_passwordCell setValue:@""];
	[[m_passwordCell textField] setAutoCapsType:0];
	[[m_passwordCell textField] setSecure:YES];
}

- (void)dealloc
{
	[m_navBar release];
	[m_messageCell release];
	[m_loginCell release];
	[m_passwordCell release];
	[m_keyboard release];
	[m_mainTable release];
	[super dealloc];
}

@end // TwitterView
